package com.starinside.wp.controller.api;

import com.starinside.wp.components.MemVerificationCache;
import com.starinside.wp.domain.WxUser;
import com.starinside.wp.domain.growth.GrowthObservation;
import com.starinside.wp.domain.growth.GrowthObservationRecord;
import com.starinside.wp.domain.growth.PO.GrowthObservationCountPO;
import com.starinside.wp.domain.growth.PO.GrowthObservationRecordPO;
import com.starinside.wp.domain.growth.VO.GrowthObservationRecordVO;
import com.starinside.wp.foundation.constant.RequestConstant;
import com.starinside.wp.foundation.model.StatusResponse;
import com.starinside.wp.foundation.query.base.PagerQuery;
import com.starinside.wp.foundation.vo.BindedBabyVO;
import com.starinside.wp.service.ChildrenService;
import com.starinside.wp.service.GrowthObservationService;
import com.tuo51.foudation.utils.CopyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;



@RestController
@RequestMapping("/wx/api/milepost_record")
@Api(value ="/wx/api/milepost_record", tags = "成长观察 - 学生记录")
public class APIGrowthObservationRecordController {

    @Autowired
    private GrowthObservationService growthObservationService;

    @Autowired
    protected MemVerificationCache cache;

    @Autowired
    private ChildrenService childrenService;

    @ApiOperation(value = "学生成长记录 - 列表")
    @PostMapping("/getGrowthList")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "classId", value = "班级ID", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "学生名", required = false, dataType = "string"),
    })
    public StatusResponse getGrowthList(HttpServletRequest req,
                                        @RequestHeader(RequestConstant.TICKET) String ticket,
                                        String classId, String name, PagerQuery pagerQuery) {

        WxUser loginUser = (WxUser) req.getSession().getAttribute("loginUser");
        BindedBabyVO defaultBaby = loginUser.getDefaultBaby();

        Page<GrowthObservationCountPO> list = growthObservationService.getGrowthList(defaultBaby.getAreaId(),classId,name,pagerQuery);
        return StatusResponse.success(list);
    }


    @ApiOperation(value = "学生成长 - 月配置")
    @PostMapping("/getMonthAgeGrowth")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "monthAge", value = "月龄", required = false, dataType = "int"),
    })
    public StatusResponse getMonthAgeGrowth(Integer monthAge) {
        GrowthObservation getMonthAgeGrowth   = growthObservationService.getMonthAgeGrowth(monthAge);
        return StatusResponse.success(getMonthAgeGrowth);
    }


    @ApiOperation(value = "学生成长 - 记录")
    @PostMapping("/getGrowthChildrenList")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "childrenId", value = "学生ID", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "classId", value = "班级ID", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "monthAge", value = "月龄", required = false, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "states", value = "状态：0未完成，1完成", required = false, dataType = "string"),
    })
    public StatusResponse getGrowthChildrenList(String childrenId,String classId,Integer monthAge,String states, PagerQuery pagerQuery) {
        Page<GrowthObservationRecordVO>  list = growthObservationService.getGrowthChildrenList(childrenId,classId,monthAge,states,pagerQuery);
        return StatusResponse.success(list);
    }


//    @ApiOperation(value = "学生成长记录 - 添加")
//    @PostMapping("/add")
//    public StatusResponse add(@RequestHeader(RequestConstant.TICKET) String ticket,GrowthObservationRecordAllPO growthObservationRecord) {
//        StaffDTO staff = cache.getStaff(ticket);
//        growthObservationService.addAll( staff,growthObservationRecord,staff.getRealName());
//        return StatusResponse.success("添加成功");
//    }
//
//
//    @ApiOperation(value = "里程碑 - 列表")
//    @PostMapping("/getMileageList")
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
//    })
//    public StatusResponse getMileageList(@RequestHeader(RequestConstant.TICKET) String ticket, PagerQuery pagerQuery) {
//        String areaId = cache.getAreaId(ticket);
//        List<MilepostV2PO>  list = growthObservationService.getMileageList(areaId,pagerQuery);
//        return StatusResponse.success(list);
//    }

    @ApiOperation(value = "里程碑 - 生成小卡")
    @PostMapping("/addUrl")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "recordId", value = "记录ID", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "url", value = "卡图片路径", required = true, dataType = "string"),
    })
    public StatusResponse addUrl(String recordId, String  url) {
        GrowthObservationRecord growthObservationRecord  = growthObservationService.update(recordId,url);
        return StatusResponse.success(growthObservationRecord);
    }


    @ApiOperation(value = "里程碑 - 详情")
    @PostMapping("/get")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "recordId", value = "记录ID", required = true, dataType = "string"),
    })
    public StatusResponse get(String recordId) {
        GrowthObservationRecord growthObservationRecord  = growthObservationService.getId(recordId);
        GrowthObservationRecordPO po = new GrowthObservationRecordPO();
        CopyUtils.copyProperties(growthObservationRecord,po);
        po.setChildren(childrenService.getById(po.getChildrenId()));
        return StatusResponse.success(po);
    }
}
